// Agent harvester in project skygarden

/* Initial beliefs and rules */

// so cai thu hoach duoc
available(vegetable,0).
// gioi han so cai trong mot lan di thu hoach
limit(vegetable,8).
// kiem tra khi nao het cai tren ruong
counter(18,0).

need_sale :- math.getVegAtStore(N) & N >= 30.

finished :- counter(G,R) & G == 0 & R==0.

enough :- available(vegetable,N) & limit(vegetable,L) & N==L.

at_vegetable(X,Y) :- pos(AgX,AgY) & AgX==X & AgY==Y.


/* Initial goals */
	
+come_with_me : not ready
	<-	+ready;
		?counter(G,R);
		-+counter(G-1,R+1);
		!goto_gate.
		
+come_with_me : true
	<- 	?counter(G,R);
		-+counter(G-1,R+1).
		
+im_died : true
	<-	?counter(G,R);
		-+counter(G,R-1).
		
/* Plans */

+!goto_gate : not at(r2,garden)
	<-	move_to(r2,garden);
		!goto_gate.
		
+!goto_gate : at(r2,garden)
	<-	.print("I'm at garden. Looking for red plants...");
		!find_to_harvest.
	
+!find_to_harvest : not enough & pos(AgX,AgY) & math.closest_vegetable(AgX,AgY,2,X,Y) & not at_vegetable(X,Y)
	<-	math.get_direction(1,AgX,AgY,X,Y,D);
		do(D);
		!find_to_harvest.
		
+!find_to_harvest : not enough & pos(AgX,AgY) & math.get_Ag_At(AgX,AgY,V) & .desire(changecolor(gray,AgX,AgY)[source(V)])
	<-	math.closest_vegetable(AgX,AgY,2,X1,Y1);
		math.get_direction(1,AgX,AgY,X1,Y1,D);
		do(D);
		!find_to_harvest.

+!find_to_harvest : not enough & pos(AgX,AgY) & math.closest_vegetable(AgX,AgY,2,X,Y)
	<-	?setOfVeg(S);
		.send(S,achieve,who_die(AgX,AgY));
		?counter(G,R);
		-+counter(G,R-1);
		?available(vegetable,N);
		-+available(vegetable,N+1);
		planted(AgX,AgY);
		.print("I got ",N+1," vegetables.(^^)");
		math.get_direction(1,AgX,AgY,X,Y,D);
		do(D);
		!find_to_harvest.
	
+!find_to_harvest : not enough & not finished
	<-	.print("I'm waiting for planting other vegetable...");
		.wait("+come_with_me");
		!find_to_harvest.
	
+!find_to_harvest : not enough & finished
	<-	.print("All vegetables is now clear!!");
		!goto_home.
	
+!find_to_harvest : enough
	<-	.print("Op! I can't carry more vegetables ! I'm going to store.");
		!goto_home.

+!goto_home : not at(r2,home) & pos(X,Y)
	<-	math.get_direction(0,X,Y,17,0,D); // 0: go home ; 1 : go gate
		do(D);
		!goto_home.

+!goto_home : at(r2,home) & pos(X,Y) & math.closest_vegetable(X,Y,2,_,_)
	<-	-+available(vegetable,0);
		drop_vegetable(r2);
		!find_to_harvest.
		
+!goto_home : at(r2,home) & not need_sale & not saled
	<-	!completed;
		drop_vegetable(r2);
		.print("Finished.").
		
+!goto_home : at(r2,home) & not need_sale & saled
	<-	-saled;
		.print("Saled.").
		
+!goto_home : need_sale
	<-	.print("Now bring all vegetables to supermarket to sale.");
		!completed;
		update_store(r2,0);
		!goto_sale.
		
+!goto_sale : not at(r2,supermarket)
	<-	move_to(r2,supermarket);
		!goto_sale.
		
+!goto_sale : at(r2,supermarket)
	<-	.print("Exporting....");
		reset(veg);
		.wait(2000);
		+saled;
		!goto_home.
	
+!completed : true
	<-	-ready;
		-+available(vegetable,0);
		-+counter(18,0);
		.abolish(setOfVeg(_));
		.abolish(come_with_me);
		.abolish(im_died).
     